﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNShapes;
using CNShapeViewer;
using BobcatUtility;

namespace CNShapeEditor
{
    /// <summary>
    /// This control shows a vertical list of thumbnails of the pages in a session.
    /// It uses the ControlStripControl fro BobcatUtility to show the thumbnails.
    /// </summary><remarks>
    /// This control works with the Session object to show the thumbnails and also
    /// to select the active page from the set of thumbnails when one of them is 
    /// clicked.
    /// </remarks>
    public partial class SessionThumbnailsControl : UserControl
    {
        #region Public properties

        /// <summary>
        /// Property to get or set the session whose pages we show.
        /// </summary>
        public Session Session
        {
            get 
            { 
                return m_Session; 
            }
            set 
            { 
                // We store the session and show its current items...
                m_Session = value;
                showThumbnails();

                // We register for events so that we can show the current 
                // state when changes are made to the session...
                if (m_Session == null)
                {
                    return;
                }
                m_Session.ActivePageChangedEvent += onActivePageChanged;
                m_Session.PageCollectionChangedEvent += onPageCollectionChanged;
            }
        }

        /// <summary>
        /// Property to set the image shown as the background for the thumbnails.
        /// </summary>
        public Image ThumbnailBackgroundImage
        {
            set
            {
                m_ThumbnailBackgroundImage = value;
                showThumbnails();
            }
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Constructor.
        /// </summary>
        public SessionThumbnailsControl()
        {
            InitializeComponent();
        }

        #endregion

        #region Private functions

        /// <summary>
        /// Called when the collection of pages in the session changes.
        /// We show the new collection.
        /// </summary>
        void onPageCollectionChanged(object sender, Session.Args e)
        {
            showThumbnails();
        }

        /// <summary>
        /// Called when the active-page has changed. 
        /// </summary>
        void onActivePageChanged(object sender, Session.Args e)
        {
            selectActivePage();
        }

        /// <summary>
        /// Shows thumbnails for the active session.
        /// </summary>
        private void showThumbnails()
        {
            // We clear out any existing thumbnails...
            ctrlControlStrip.Clear();

            // We show new ones...
            if(m_Session == null)
            {
                return;
            }
            foreach (Page page in m_Session.Pages)
            {
                // We create a ShapeViewer control for each one...
                ShapeViewerControl ctrlShapeViewer = new ShapeViewerControl();
                ctrlShapeViewer.Width = SHAPEVIEWER_WIDTH;
                ctrlShapeViewer.Height = SHAPEVIEWER_HEIGHT;
                ctrlShapeViewer.BackgroundImage = m_ThumbnailBackgroundImage;
                ctrlShapeViewer.AutoZoom = true;
                ctrlShapeViewer.Page = page;

                // And add it to the control...
                ctrlControlStrip.addControl(ctrlShapeViewer);
            }

            // We select the active one...
            selectActivePage();
        }

        /// <summary>
        /// Makes sure that the active page is selected in the control-strip.
        /// </summary>
        private void selectActivePage()
        {
            // We find the ShapeViewer control in the control strip for the
            // active page in the session...
            ShapeViewerControl controlForActivePage = null;
            foreach (Control control in ctrlControlStrip.Controls)
            {
                ShapeViewerControl shapeViewerControl = (ShapeViewerControl)control;
                if (shapeViewerControl.Page == m_Session.ActivePage)
                {
                    controlForActivePage = shapeViewerControl;
                    break;
                }
            }
            if (controlForActivePage == null)
            {
                throw new Exception("Control for active page not found in the session thumbnail control.");
            }

            // If this is not the active selection, we select it...
            if (controlForActivePage != ctrlControlStrip.SelectedControl)
            {
                ctrlControlStrip.SelectedControl = controlForActivePage;
            }
        }

        /// <summary>
        /// Called when the selected page changes. 
        /// </summary>
        private void onSelectionChanged(object sender, ControlStripControl.Args e)
        {
            // We find which page was selected...
            ShapeViewerControl shapeViewerControl = (ShapeViewerControl)e.Control;
            Page page = shapeViewerControl.Page;

            // We set it as the active page (if it is not already the active page)...
            if(m_Session.ActivePage != page)
            {
                m_Session.ActivePage = page;
            }
        }

        #endregion

        #region Private data

        // Constants...
        private int SHAPEVIEWER_WIDTH = 160;
        private int SHAPEVIEWER_HEIGHT = 90;

        // The session observed and managed by this control.
        private Session m_Session = null;

        private Image m_ThumbnailBackgroundImage = null;

        #endregion
    }
}
